﻿@model Web.ViewModels.ShoppingCartViewModel
@{
    ViewBag.Title = "Корзина";
}
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="../../Scripts/CleanCart.js"></script>
<script type="text/javascript">
    $(function () {
        // Document.ready -> link up remove event handler
        $(".RemoveLink").click(function () {
            // Get the id from the link
            var recordToDelete = $(this).attr("data-id");

            if (recordToDelete != '') {

                // Perform the ajax post
                $.post("/ShoppingCart/RemoveFromCart", { "id": recordToDelete },
                    function (data) {
                        // Successful requests get here
                        // Update the page elements
                        if (data.ItemCount == 0) {
                            $('#row-' + data.DeleteId).fadeOut('slow');
                        } else {
                            $('#item-count-' + data.DeleteId).text(data.ItemCount);
                        }

                        $('#cart-total').text(data.CartTotal);
                        $('#update-message').text(data.Message).fadeOut(2000);
                        $('#cart-status').text('Товаров в корзине (' + data.CartCount + ')');
                    });
            }

        });



    });


    function handleUpdate() {
        // Load and deserialize the returned JSON data
        var json = context.get_data();
        var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);

        // Update the page elements
        if (data.ItemCount == 0) {
            $('#row-' + data.DeleteId).fadeOut('slow');
        } else {
            $('#item-count-' + data.DeleteId).text(data.ItemCount);
        }

        $('#cart-total').text(data.CartTotal);
        $('#update-message').text(data.Message);
        $('#cart-status').text('Товаров в корзине (' + data.CartCount + ')');
    }
</script>

<div class="content_in">
    <h3>Просмотр корзины
    </h3>

    @if (@Model.CartItems.Count == 0)
    {
        <p class="update">
            Ваша корзина пуста
        </p>
    }
    else
    {
        <div class="button order">
            @Html.ActionLink("Оформить заказ", "AddressAndPayment","Checkout")
        </div>
        
        <div class="button clear_order">
            @Html.ActionLink("Очистить корзину", "CleanCart",null ,new { @class = "CleanLink"})
        </div>
    
        <div style="clear: both"></div>
    
        <div id="update-message" class="update">
        </div>
        <table class="trash">
            <tr>
                <th>Название
                </th>
                <th>Упаковка
                </th>
                <th>Цена (грн)
                </th>

                <th>Количество
                </th>
                <th></th>
            </tr>
            @foreach (var item in Model.CartItems)
            {
                <tr id="row-@item.Id">
                    <td>
                        @item.Produce.Name
                    </td>
                    <td> 
                        @item.Prepackage.Weight
                    </td>
                    <td>
                        @String.Format("{0:0.00}", ( item.Count*item.Price))
                        
                    </td>
                    <td id="item-count-@item.Id">
                        @item.Count
                    </td>
                    <td>
                        <a href="#" class="RemoveLink" data-id="@item.Id">удалить</a>
                    </td>
                </tr>
            }
            <tr>
                <td>Общая сумма (грн)
                </td>
                <td id="cart-total">
                     @Model.CartTotal.ToString("F2")
                   
                </td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    
    

    }
</div>
